
var newer = newer || {};
newer.gd = newer.gd || {};

newer.OverviewDiv = function(o) {
	this.domNode = o.domNode;
	this.prt = o.prt;
	
	this.jcrop = {};
	
	this.init();
}

newer.OverviewDiv.prototype = {
	init: function() {
		this.domNode.html('<div id="overview-avatar-div"></div><div id="portrait-upload-div" ></div>');
		
		this.avatarNode = $("#overview-avatar-div");
		this.avatarNode.html('<img id="avatar-img" width=180px/><div id="avatar-img-mask"></div><div id="portrait-crop-div"><img id="avatar-crop-img"/><div id="avatar-crop-submit-div">Submit</div></div>');
		
		this.uploadNode = $("#portrait-upload-div").hide();
		this.initUploadButton();
		
		$("#portrait-crop-div").dialog({
			autoOpen: false,
			modal: true,
			draggable:false,
			dialogClass:"no-title-dialog",
			resizable:false,
			zIndex:5000,
			closeOnEscape:false,
			width:"auto"
		});
		
		this.domNode.removeClass("nw-event-listener").addClass("nw-event-listener");
		this.domNode.off("nw-event").on("nw-event", $.proxy(this.nweHandler,this));
		
		this.refresh();
	},
	
	refresh: function() {
		this.updateAvatar();
		this.updateUploadButton();
	},
	
	nweHandler: function(e,c) {
		if (c.type == newer.EventHelper.PAGE_TYPE) {
			if (c.fromStatus.ptype == c.toStatus.ptype) {
				this.refresh();
			}
		} else if (c.type == newer.EventHelper.AUTH_TYPE) {
			this.updateUploadButton();
		}
	},
	
	initUploadButton: function() {
		var self = this;
		uploader = new qq.FileUploader({
			element: document.getElementById("portrait-upload-div"),
			action: '/Photos/uploadUserPhoto', 
			allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'], 
			onComplete: function(id, fileName, responseJSON){
				newer.gd.people.hasPhoto=true;
				self.cropAvatar(responseJSON);
				//xself.updateAvatar();
			}
		});
	},
	
	updateUploadButton: function() {	
		if(newer.gd.pstatus.ptype == newer.UriHelper.PEOPLE_TYPE) {
			if(newer.gd.user.id != -1 && newer.gd.user.id == newer.gd.people.id) {
				this.uploadNode.show();	
			} else {
				this.uploadNode.hide();
			}
		} else {
			this.uploadNode.hide();
		}
	},
	
	cropAvatar: function(result) {
		var self = this;
		var people = newer.gd.people;
		$("#avatar-crop-img").attr("src","/Photos/displayUserPhotoThumbnail?id="+people.id+"&s="+Math.random());
		$("#avatar-crop-img").Jcrop({
			aspectRatio: 1
		}, function() {
			self.jcrop = this;
			self.jcrop.animateTo([0,0,50,50]);
		});
		
		var x = ($(window).width()-result.w) / 2;
		var y = 20;
		$("#portrait-crop-div").dialog('option','position',[x,y]);
		
		$("#avatar-crop-submit-div").on("click", function() {
			var s = self.jcrop.tellSelect();
			$.post("/Photos/cropUserPhoto?uid="+newer.gd.user.id+"&x="+s.x+"&y="+s.y+"&w="+s.w+"&h="+s.h, {}, function() {
				self.updateAvatar();
				$("#portrait-crop-div").dialog('close');
				self.jcrop.destroy();
			}, "json");
		});
		$("#portrait-crop-div").dialog('open');
		
	},
	
	updateCropRect: function(c) {
		this.cropRect = [c.x,c.y,c.w,c.h];
	},
	
	updateAvatar: function() {
		switch(newer.gd.pstatus.ptype) {
			case newer.UriHelper.PEOPLE_TYPE:
				this.updatePeopleAvatar();
				break;
			case newer.UriHelper.CIRCLE_TYPE:
				this.updateCircleAvatar();
				break;
			default:
				// nothing
		}
	},
	
	updatePeopleAvatar: function() {
		var people = newer.gd.people;
		if(people.hasPhoto==true) {
			$("img#avatar-img").attr('src', "/Photos/displayUserPhoto?id="+people.id+"&s="+Math.random());
		} else {
			$("img#avatar-img").attr('src', "/public/images/default-user-image");
		}

	},
	
	updateCircleAvatar: function() {
		var circle = newer.gd.circle;
		if(circle.hasPhoto==true) {
			$("img#avatar-img").attr('src', "/Circles/displayCirclePhoto?cid="+circle.id+"&s="+Math.random());
		} else if (circle.hasMap == true) {
			$("img#avatar-img").attr("src", "/Circles/displayCircleMap?cid="+circle.id+"&s="+Math.random());
		} else {
			$.post("/Circles/updateCircleMap?cid="+circle.id, {}, function(){
				circle.hasMap = true;
				$("img#avatar-img").attr("src", "/Circles/displayCircleMap?cid="+circle.id+"&s="+Math.random());
			})
		}
	}
}
